package com.project.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.entity.po.Member;
import com.project.entity.qo.MemberQo;
import com.project.exception.ServiceException;
import com.project.mapper.MemberMapper;
import com.project.mapper.UserMapper;
import com.project.result.ResponseResult;
import com.project.result.ResultCode;
import com.project.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ProjectName:
 * @Package:
 * @ClassName:
 * @Author: ZhaoLongShi
 * @CreateDate:
 */
@SuppressWarnings("all")
@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 查询会员（分页）
     *
     * @param current
     * @return
     */
    @Override
    public ResponseResult<Page<Member>> selectAllMember(int current) {
        Page<Member> members = memberMapper.selectAllMember(current);
        if (ObjectUtils.isEmpty(members)) {
            throw new ServiceException(ResultCode.PERMISSION_NO_ACCESS);
        }
        return ResponseResult.success(members);

    }

    /**
     * 根据类型查询会员
     *
     * @param cardType
     * @return
     */
    @Override
    public ResponseResult<List<Member>> selectByType(int cardType) {
        List<Member> members = memberMapper.selectByType(cardType);
        if (ObjectUtils.isEmpty(members)) {
            throw new ServiceException(ResultCode.PERMISSION_NO_ACCESS);
        }
        return ResponseResult.success(members);
    }

    /**
     * 删除
     *
     * @param idList
     * @return
     */
    @Override
    public ResponseResult<ResultCode> deleteMemberByIds(List<Long> idList) {
        if (idList == null) {
            throw new ServiceException(ResultCode.PARAMS_NOT_IS_BLANK);
        }
        int count = memberMapper.deleteMemberByIds(idList);
        if (count == 0) {
            throw new ServiceException(ResultCode.PERMISSION_NO_ACCESS);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 添加会员
     *
     * @param memberQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> addMember(MemberQo memberQo) {
        Member member = new Member();
        BeanUtils.copyProperties(memberQo, member);
        Long aLong = userMapper.isVip(memberQo.getUserId());
        if (aLong == 1) {
            throw new ServiceException(ResultCode.IS_VIP);
        }
        int count = memberMapper.insert(member);
        if (count != 1) {
            throw new ServiceException(ResultCode.INSERT_FAILED);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 更新用户
     *
     * @param memberQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> updateMemberById(MemberQo memberQo) {
        Member member = new Member();
        BeanUtils.copyProperties(memberQo, member);
        int count = memberMapper.updateById(member);
        if (count == 0) {
            throw new ServiceException(ResultCode.PERMISSION_NO_ACCESS);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }
}
